package com.ruoyi.pdf.receipt;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.ruoyi.common.core.domain.entity.SysDictData;
import com.ruoyi.labdanger.domain.LabApplyOccupation;
import com.ruoyi.labdanger.domain.LabApplyOccupationDetails;
import com.ruoyi.labdanger.service.ILabApplyOccupationDetailsService;
import com.ruoyi.labdanger.service.ILabApplyOccupationService;
import com.ruoyi.pdf.PdfGenerateServiceImpl;
import com.ruoyi.system.service.ISysDeptService;
import com.ruoyi.system.service.ISysDictDataService;
import com.ruoyi.system.service.ISysUserService;

@Component
public class PdfGeneratorOccupation implements PdfGenerator {

    public String getFileName(Long instanceId){
    	return "IAM易制毒品领取登记单("+instanceId+")";
    }
	@Autowired
    private PdfGenerateServiceImpl pdfGenerateService;
	@Autowired
	private ILabApplyOccupationService labApplyOccupationService;
	@Autowired
	private ILabApplyOccupationDetailsService labApplyOccupationDetailsService;
    @Autowired
    private ISysDictDataService sysDictDataService;
    public void run(Long instanceId) throws Exception {
    	
    	Map<String, Object> data = new HashMap<>();
    	SysDictData queryDict = new SysDictData();
    	queryDict.setDictType("unitOpt");
    	List<SysDictData> dictList = 
    	sysDictDataService.selectDictDataList(queryDict); 
    	LabApplyOccupation labApplyOccupation = labApplyOccupationService.selectLabApplyOccupationById(instanceId);
    	if(labApplyOccupation==null){
    		labApplyOccupation = new LabApplyOccupation();
    	}
    	data.put("data", labApplyOccupation);
    	LabApplyOccupationDetails query = new LabApplyOccupationDetails();
    	query.setInstanceId(instanceId);
    	List<LabApplyOccupationDetails> detailList = labApplyOccupationDetailsService.selectLabApplyOccupationDetailsList(query);
    	for(LabApplyOccupationDetails details : detailList){
    		for(SysDictData dict : dictList){
    			if(Long.valueOf(dict.getDictValue())==details.getUnitOpt().longValue()){
    				details.setModel(dict.getDictLabel());
    				break;
    			}
    		}
    	}
    	data.put("items", detailList);
    	
        pdfGenerateService.generatePdfFile("danger_occupation", data, getFileName(instanceId)+".pdf");
    }
}
